from app import app, db
from datetime import datetime
from werkzeug.security import generate_password_hash

# 简单模型定义
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))
    role = db.Column(db.String(20), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    is_active = db.Column(db.Boolean, default=True)

class Student(User):
    __tablename__ = 'student'
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    full_name = db.Column(db.String(100), nullable=False)
    student_id = db.Column(db.String(20), unique=True, nullable=False)
    major = db.Column(db.String(100), nullable=False)
    grade = db.Column(db.String(20))

def create_users():
    with app.app_context():
        print("检查数据库表...")
        
        # 检查表是否存在
        from sqlalchemy import inspect
        inspector = inspect(db.engine)
        if 'user' not in inspector.get_table_names():
            print("创建数据库表...")
            db.create_all()
        
        # 检查用户是否已存在
        if User.query.filter_by(username='student1').first():
            print("用户已存在，跳过创建")
            return
        
        print("创建测试用户...")
        
        # 创建简单用户（避免复杂继承）
        user1 = User(
            username='student1',
            email='student1@example.com',
            role='student'
        )
        user1.set_password('password123')
        
        user2 = User(
            username='teacher1', 
            email='teacher1@example.com',
            role='teacher'
        )
        user2.set_password('password123')
        
        user3 = User(
            username='company1',
            email='company1@example.com', 
            role='enterprise'
        )
        user3.set_password('password123')
        
        db.session.add(user1)
        db.session.add(user2)
        db.session.add(user3)
        db.session.commit()
        
        print("测试用户创建成功!")
        print("student1 / password123")
        print("teacher1 / password123") 
        print("company1 / password123")

if __name__ == '__main__':
    create_users()